// OutputPDF.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "OutputPDF.h"
#include <hpdf.h>
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <setjmp.h>
#include "hpdf.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
BOOL	generatePDF();

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_OUTPUTPDF, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_OUTPUTPDF));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OUTPUTPDF));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_OUTPUTPDF);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			generatePDF();
			//DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}


jmp_buf env;

#ifdef HPDF_DLL
void  __stdcall
#else
void
#endif
	error_handler  (HPDF_STATUS   error_no,
	HPDF_STATUS   detail_no,
	void         *user_data)
{
	printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
		(HPDF_UINT)detail_no);
	longjmp(env, 1);
}

const HPDF_BYTE RAW_IMAGE_DATA[128] = {
	0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xfc,
	0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xf0,
	0xf3, 0xf3, 0xff, 0xe0, 0xf3, 0xf3, 0xff, 0xc0,
	0xf3, 0xf3, 0xff, 0x80, 0xf3, 0x33, 0xff, 0x00,
	0xf3, 0x33, 0xfe, 0x00, 0xf3, 0x33, 0xfc, 0x00,
	0xf8, 0x07, 0xf8, 0x00, 0xf8, 0x07, 0xf0, 0x00,
	0xfc, 0xcf, 0xe0, 0x00, 0xfc, 0xcf, 0xc0, 0x00,
	0xff, 0xff, 0x80, 0x00, 0xff, 0xff, 0x00, 0x00,
	0xff, 0xfe, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00,
	0xff, 0xf8, 0x0f, 0xe0, 0xff, 0xf0, 0x0f, 0xe0,
	0xff, 0xe0, 0x0c, 0x30, 0xff, 0xc0, 0x0c, 0x30,
	0xff, 0x80, 0x0f, 0xe0, 0xff, 0x00, 0x0f, 0xe0,
	0xfe, 0x00, 0x0c, 0x30, 0xfc, 0x00, 0x0c, 0x30,
	0xf8, 0x00, 0x0f, 0xe0, 0xf0, 0x00, 0x0f, 0xe0,
	0xe0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00,
	0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

BOOL generatePDF()
{
	HPDF_Doc  pdf;
	HPDF_Font font;
	HPDF_Page page;
	char fname[256] = "e:\\test.pdf";
	HPDF_Image image;

	HPDF_REAL x, y;

	//strcpy (fname, argv[0]);
	//strcat (fname, ".pdf");

	pdf = HPDF_New (error_handler, NULL);
	if (!pdf) {
		printf ("error: cannot create PdfDoc object\n");
		return 1;
	}

	/* error-handler */
	if (setjmp(env)) {
		HPDF_Free (pdf);
		return 1;
	}

	HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);

	/* create default-font */
	font = HPDF_GetFont (pdf, "Helvetica", NULL);

	/* add a new page object. */
	page = HPDF_AddPage (pdf);

	/*HPDF_Page_SetWidth (page, 172);
	HPDF_Page_SetHeight (page, 80);*/
	HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_LANDSCAPE);

	HPDF_Page_BeginText (page);
	HPDF_Page_SetFontAndSize (page, font, 20);
	HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
	HPDF_Page_ShowText (page, "RawImageDemo");
	HPDF_Page_EndText (page);

	/* load RGB raw-image file. */
#ifndef __WIN32__
	//HPDF_LoadRawImageFromMem(pdf, RAW_IMAGE_DATA, 32, 32, )
	image = HPDF_LoadJpegImageFromFile(pdf, "e:/test.jpg");
	/*image = HPDF_LoadRawImageFromFile (pdf, "e:/rawimage/32_32_rgb.dat",
		32, 32, HPDF_CS_DEVICE_RGB);*/
#else
	image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_rgb.dat",
		32, 32, HPDF_CS_DEVICE_RGB);
#endif

	x = 20;
	y = 20;

	/* Draw image to the canvas. (normal-mode with actual size.)*/
	HPDF_Page_DrawImage (page, image, x, y, 500, 500);

page = HPDF_AddPage (pdf);

	/*HPDF_Page_SetWidth (page, 172);
	HPDF_Page_SetHeight (page, 80);*/
	HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_LANDSCAPE);

	HPDF_Page_BeginText (page);
	HPDF_Page_SetFontAndSize (page, font, 20);
	HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
	HPDF_Page_ShowText (page, "RawImageDemo");
	HPDF_Page_EndText (page);

	/* load RGB raw-image file. */
#ifndef __WIN32__
	//HPDF_LoadRawImageFromMem(pdf, RAW_IMAGE_DATA, 32, 32, )
	image = HPDF_LoadJpegImageFromFile(pdf, "e:/test.jpg");
	/*image = HPDF_LoadRawImageFromFile (pdf, "e:/rawimage/32_32_rgb.dat",
		32, 32, HPDF_CS_DEVICE_RGB);*/
#else
#endif

	x = 20;
	y = 20;

	/* Draw image to the canvas. (normal-mode with actual size.)*/
	HPDF_Page_DrawImage (page, image, x, y, 500, 500);

	/* load GrayScale raw-image file. */
//#ifndef __WIN32__
//	image = HPDF_LoadRawImageFromFile (pdf, "e:/rawimage/32_32_gray.dat",
//		32, 32, HPDF_CS_DEVICE_GRAY);
//#else
//	image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_gray.dat",
//		32, 32, HPDF_CS_DEVICE_GRAY);
//#endif
//
//	x = 70;
//	y = 20;
//
//	/* Draw image to the canvas. (normal-mode with actual size.)*/
//	HPDF_Page_DrawImage (page, image, x, y, 500, 500);
//
	/* load GrayScale raw-image (1bit) file from memory. */
	//image = HPDF_LoadRawImageFromMem (pdf, RAW_IMAGE_DATA, 32, 32,
	//	HPDF_CS_DEVICE_GRAY, 1);

	//x = 120;
	//y = 20;

	/* Draw image to the canvas. (normal-mode with actual size.)*/
	//HPDF_Page_DrawImage (page, image, x, y, 32, 32);

	/* save the document to a file */
	HPDF_SaveToFile (pdf, fname);

	/* clean up */
	HPDF_Free (pdf);

	return 0;
}